home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3641 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Type conversion
  5. Date: Tue, 30 Jan 1996 14:55:47 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <310E1553.512C@cmt.lpr.mail.carel.fi>
  8. References: <4ejjka$2rf@news3.cts.com>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Jim Fitzgerald wrote:
  16. > Hi,
  17. >   Was wondering if a guru out there could tell me why the following
  18. > code fragment for extracting an long from a buffer does not work.
  19. > Following the bad code, is a fragment that does work.. but is fairly
  20. > cheezy! )..
  21. > This code compiles and runs but returns INCORRECT results:
  22. > char buffer[512];
  23. > long  node_left, node_right;
  24. > {
  25. >   node_left  =  (long)*(buffer+4);
  26. >   node_right=  (long)*(buffer+8);
  27. > }
  28. > This code runs and returns CORRECT results:
  29. > char buffer[512];
  30. > long node_left, node_right;
  31. > long *bogus;
  32. > {
  33. >   bogus = buffer+4;
  34. >   node_left = *bogus;
  35. >   bogus = buffer+8;
  36. >   node_right= *bogus;
  37. > }
  38. > Thanks!
  39. > -Jim
  40.  
  41. This should also work:
  42. char buffer[512];
  43. long  node_left, node_right;
  44. {
  45.   node_left  =  *((long*)(buffer+4));
  46.   node_right=  *((long*)(buffer+8));
  47. }
  48.  
  49.  
  50. -- 
  51. All my opinions are mine and mine alone.
  52.